package com.sheng.leetcode.year2022.swordfingeroffer.day25;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author liusheng
 * @date 2022/09/23
 *<p>
 * 剑指 Offer 29. 顺时针打印矩阵<p>
 *<p>
 * 输入一个矩阵，按照从外向里以顺时针的顺序依次打印出每一个数字。<p>
 *<p>
 * 示例 1：<p>
 * 输入：matrix = [[1,2,3],[4,5,6],[7,8,9]]<p>
 * 输出：[1,2,3,6,9,8,7,4,5]<p>
 *<p>
 * 示例 2：<p>
 * 输入：matrix =[[1,2,3,4],[5,6,7,8],[9,10,11,12]]<p>
 * 输出：[1,2,3,4,8,12,11,10,9,5,6,7]<p>
 *<p>
 * 限制：<p>
 *<p>
 * 0 <= matrix.length <= 100<p>
 * 0 <= matrix[i].length<= 100<p>
 * 注意：本题与主站 54 题相同：<a href="https://leetcode-cn.com/problems/spiral-matrix/">...</a><p>
 *<p>
 * 来源：力扣（LeetCode）<p>
 * 链接：<a href="https://leetcode.cn/problems/shun-shi-zhen-da-yin-ju-zhen-lcof">...</a><p>
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。<p>
 */
public class Sword0029 {

    @Test
    public void test01() {
//        int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}};
//        int[][] matrix = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
//        int[][] matrix = {{3},{2}};
//        int[][] matrix = {{6,9,7}};
//        int[][] matrix = {{2,5,8},{4,0,-1}};
        int[][] matrix = {{1,11},{2,12},{3,13},{4,14},{5,15},{6,16},{7,17},{8,18},{9,19},{10,20}};
        System.out.println(Arrays.toString(new Solution().spiralOrder(matrix)));
    }
}
class Solution {
    public int[] spiralOrder(int[][] matrix) {
        if (matrix.length == 0) {
            return new int[0];
        }
        int m = matrix.length;
        int n = matrix[0].length;
        List<Integer> list = new ArrayList<>();
        int x = 0, y = 0;
        while (x <= m / 2 && y <= n / 2) {
            if ((n == 2 || m == 2) && x == 1 && y == 1) {
                x++;
                y++;
                break;
            }
            // 从左到右
            int j = y;
            for ( ; j < n - y; j++) {
                list.add(matrix[x][j]);
            }
            // 从上到下
            j--;
            int i = x;
            for (i += 1; i < m - x; i++) {
                list.add(matrix[i][j]);
            }
            i--;
            if (j > y && i > x) {
                // 从右到左
                for (j -= 1; j > y; j--) {
                    list.add(matrix[i][j]);
                }
                // 从下到上
                for (; i > x; i--) {
                    list.add(matrix[i][j]);
                }
            }
            x++;
            y++;
        }
        int[] ints = list.stream().mapToInt(Integer::intValue).toArray();
        return ints;
    }
}

//class Solution {
//    public int[] spiralOrder(int[][] matrix) {
//        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
//            return new int[0];
//        }
//        int rows = matrix.length, columns = matrix[0].length;
//        int[] order = new int[rows * columns];
//        int index = 0;
//        int left = 0, right = columns - 1, top = 0, bottom = rows - 1;
//        while (left <= right && top <= bottom) {
//            for (int column = left; column <= right; column++) {
//                order[index++] = matrix[top][column];
//            }
//            for (int row = top + 1; row <= bottom; row++) {
//                order[index++] = matrix[row][right];
//            }
//            if (left < right && top < bottom) {
//                for (int column = right - 1; column > left; column--) {
//                    order[index++] = matrix[bottom][column];
//                }
//                for (int row = bottom; row > top; row--) {
//                    order[index++] = matrix[row][left];
//                }
//            }
//            left++;
//            right--;
//            top++;
//            bottom--;
//        }
//        return order;
//    }
//}
//
//作者：LeetCode-Solution
//链接：https://leetcode.cn/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/solution/shun-shi-zhen-da-yin-ju-zhen-by-leetcode-solution/
//来源：力扣（LeetCode）
//著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
